package com.demo.utils;

import org.junit.Test;
import org.springframework.util.CollectionUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author gy
 * @date 2023/11/27
 */
public class DateUtils2 {
    @Test
    public void m1() throws ParseException {
        long startTime = System.currentTimeMillis();

        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date date = sdf.parse("2023-08-31 18:35:10");

        String currentDayStartTime = getCurrentDayStartTime(date);
        String currentDayEndTime = getCurrentDayEndTime(date);

        String currentWeekStartTime = getCurrentWeekStartTime(date);
        String currentWeekEndTime = getCurrentWeekEndTime(date);

        String currentMonthStartTime = getCurrentMonthStartTime(date);
        String currentMonthEndTime = getCurrentMonthEndTime(date);


        String lastDayStartTime = getLastDayStartTime(date);
        String lastDayEndTime = getLastDayEndTime(date);

        String lastWeekStartTime = getLastWeekStartTime(date);
        String lastWeekEndTime = getLastWeekEndTime(date);

        String lastMonthStartTime = getLastMonthStartTime(date);
        String lastMonthEndTime = getLastMonthEndTime(date);

        Integer twoDayInterval = getTwoDayInterval("2024-02-21 00:00:00", "2024-02-21 13:59:59");


        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    /**
     * 获取 本日 开始时间
     *
     * @return date
     */
    public static String getCurrentDayStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return sdf.format(date);
    }

    /**
     * 获取 本日 结束时间
     *
     * @return date
     */
    public static String getCurrentDayEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        return sdf.format(date);
    }

    /**
     * 获取 本周 第一天（星期一） 开始时间
     *
     * @return date
     */
    public static String getCurrentWeekStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // Monday 这里设置一周开始时间是星期一
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 本周 星期日的 结束时间
     *
     * @return date
     */
    public static String getCurrentWeekEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.WEEK_OF_YEAR,1);
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 本月 第一天（1号） 开始时间
     *
     * @return date
     */
    public static String getCurrentMonthStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 这里设置 一个月 开始的时间为 1号
        c.set(Calendar.DAY_OF_MONTH, 1);

        return sdf.format(c.getTime());
    }

    /**
     * 获取 本月 最后一天的 结束时间
     *
     * @return date
     */
    public static String getCurrentMonthEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 这里设置 一个月 开始的时间为 1号
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));

        return sdf.format(c.getTime());
    }

    /**
     * 获取 昨天 开始时间
     *
     * @return date
     */
    public static String getLastDayStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR,-1);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 昨天 结束时间
     *
     * @return date
     */
    public static String getLastDayEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR,-1);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 上周 第一天（星期一） 开始时间
     *
     * @return date
     */
    public static String getLastWeekStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // Monday 这里设置一周开始时间是星期一
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        c.add(Calendar.WEEK_OF_YEAR,-1);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 上周 星期日的 结束时间
     *
     * @return date
     */
    public static String getLastWeekEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return sdf.format(c.getTime());
    }


    /**
     * 获取 上月 第一天（1号） 开始时间
     *
     * @return date
     */
    public static String getLastMonthStartTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 这里设置 一个月 开始的时间为 1号
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.MONTH,-1);
        return sdf.format(c.getTime());
    }


    /**
     * 获取 上月 的 结束时间
     *
     * @return date
     */
    public static String getLastMonthEndTime(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH,-1);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sdf.format(c.getTime());
    }


    /**
     * 计算两个事件相差毫秒数
     * @throws ParseException
     */
    @Test
    public void m2() throws ParseException {
        long startTime = System.currentTimeMillis();

        String t1 = "2023-08-19 00:00:00";
        String t2 = "2023-08-20 00:00:00";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = sdf.parse(t1);
        Date d2 = sdf.parse(t2);
        long result = d1.getTime()-d2.getTime();
        System.out.println(result);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }



    @Test
    public void m3() throws ParseException, InterruptedException {
        long startTime = System.currentTimeMillis();

        Date d1 = new Date();
        Thread.sleep(3000);
        Date d2 = new Date();
        boolean result = d2.after(d1);
        boolean result2 = d1.before(d2);

        System.out.println(d1.toString()+ "---" + d2.toString()+ "---" + result);
        System.out.println(d1.toString()+ "---" + d2.toString()+ "---" + result2);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    /**
     * 拼接时间轴下标
     */
    @Test
    public void m4(){
        long startTime = System.currentTimeMillis();

        GregorianCalendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        System.out.println(hour);
        for (int i = 0; i <= hour; i++) {

        }

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    /**
     * 获取 前多少秒的时间
     *
     * @return date
     */
    public static String getLastSecondTime(Date date,int secondNum) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.SECOND,-secondNum);
        return sdf.format(c.getTime());
    }


    @Test
    public void m5() throws ParseException {
        long startTime = System.currentTimeMillis();

        System.out.println(getTimeScope("2024-01-22 17:00:00"));

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }
    /**
     * 获取 下一个小时的时间
     *
     * @return startTime
     */
    public static String getTimeScope(String startTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(startTime);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR_OF_DAY,1);
        String endTime = sdf.format(c.getTime());
        return startTime.substring(11,16) + "-" + endTime.substring(11,16);
    }

    /**
     * 获取两天的时间间隔
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public Integer getTwoDayInterval(String startTime, String endTime) throws ParseException {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        //开始时间
        Date startDate = dft.parse(startTime);
        //结束时间
        Date endDate = dft.parse(endTime);
        Date nextDay = startDate;
        int i = 0;
        //当明天不在结束时间之前是终止循环
        while (nextDay.before(endDate)) {
            Calendar cld = Calendar.getInstance();
            cld.setTime(startDate);
            cld.add(Calendar.DATE, 1);
            startDate = cld.getTime();
            //获得下一天日期字符串
            nextDay = startDate;
            i++;
        }
        return i + 1;
    }

    @Test
    public void m6() throws ParseException {
        long startTime = System.currentTimeMillis();

        Integer twoDayInterval = getTwoDayInterval("2024-02-21 00:00:00", "2024-02-21 13:59:59");

        System.out.println(twoDayInterval+ "************");
        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }


    /**
     * 获取时间段
     * @param hour 00、01...23
     * @return 00:00-01:00
     */
    public static String getTimeScope2(String hour) {
        String curHourStr = hour + ":00";
        int curHour = Integer.parseInt(hour);
        int nextHour = curHour + 1;
        String nextHourStr;
        if (nextHour <= 9){
            nextHourStr = "0" + nextHour + ":00";
        }else if(nextHour > 23){
            nextHourStr = "00:00";
        }else{
            nextHourStr = nextHour + ":00";
        }
        return curHourStr + "-" + nextHourStr;
    }

    @Test
    public void m7() {
        long startTime = System.currentTimeMillis();
        System.out.println(getTimeScope2("00"));

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    @Test
    public void m8() throws ParseException {
        long startTime = System.currentTimeMillis();

        List<String> dateList = getDateListByStartAndEndDate("2023-03-01", "2023-04-01");
        System.out.println(dateList);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }


    /**
     * 通过开始时间、结束时间 获取日期列表
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public List<String> getDateListByStartAndEndDate(String startDate, String endDate) throws ParseException {

        List<String> dateList = new ArrayList<>();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar editStartPeriod = Calendar.getInstance();
        editStartPeriod.setTime(format.parse(startDate));
        Calendar editEndPeriod = Calendar.getInstance();
        editEndPeriod.setTime(format.parse(endDate));
        Calendar curr = editStartPeriod;
        while (curr.before(editEndPeriod)) {
            String dateStr = dayFormat.format(curr.getTime());
            curr.add(Calendar.DATE, 1);
            dateList.add(dateStr);
        }
        if (!CollectionUtils.isEmpty(dateList)){
            String finalDayStr = dayFormat.format(editEndPeriod.getTime());
            dateList.add(finalDayStr);
        }
        return dateList;
    }


    /**
     * 获取 前几分钟的时间
     *
     * @return date
     */
    public static String getAfterMinuteTime(Date date,int amount) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE,amount);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 后几分钟的时间
     *
     * @return date
     */
    public static String getBeforeMinuteTime(Date date, int amount) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, -amount);
        return sdf.format(c.getTime());
    }

    public static String getTimeBeforeDay(String startTime,Integer dayNum) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(startTime);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH,-dayNum);
        return sdf.format(c.getTime());
    }


    /**
     * 获取进一个小时的时间
     */
    @Test
    public void m10() throws ParseException {
        long startTime = System.currentTimeMillis();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse("2024-04-28 13:11:00");

        System.out.println(getBeforeOneHourStartTime(date));
        System.out.println(getBeforeOneHourEndTime(date));

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    /**
     * 获取 前一个小时开始时间
     *
     * @return date
     */
    public static String getBeforeOneHourStartTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR,-1);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 前一个小时结束时间
     *
     * @return date
     */
    public static String getBeforeOneHourEndTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR,-1);
        return sdf.format(c.getTime());
    }
}
